
import 'package:flutter/material.dart';
import 'package:flutter_demo/home/provider/home_provider.dart';
import 'package:flutter_demo/message/pages/message_page.dart';
import 'package:flutter_demo/my/pages/my_page.dart';
import 'package:flutter_demo/point/pages/point_page.dart';
import 'package:flutter_demo/res/resources.dart';
import 'package:flutter_demo/scene/pages/scene_page.dart';
import 'package:flutter_demo/util/double_tap_back_exit_app.dart';
import 'package:flutter_demo/util/theme_utils.dart';
import 'package:flutter_demo/widgets/load_image.dart';
import 'package:provider/provider.dart';

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  var _pageList;
    
  final _pageController = PageController();

  HomeProvider provider = HomeProvider();

  @override
  void initState() {
    super.initState();
    initData();
  }

  void initData(){
    _pageList =[
      ScenePage(),
      PointPage(),
      PointPage(),
      MessagePage(),
      MyPage(),
    ];
  }


  @override
  Widget build(BuildContext context) {
    // bool isDark = ThemeUtils.isDark(context);
    // double itemWidth = MediaQuery.of(context).size.width / 5;

    return ChangeNotifierProvider<HomeProvider>(
      create: (_) => provider,
      child: DoubleTapBackExitApp(
        child: Scaffold(
          bottomNavigationBar: Consumer<HomeProvider>(
            builder: (_,provider,__){
              return BottomNavigationBar(
                elevation: 5.0,
                type:BottomNavigationBarType.fixed,
                unselectedFontSize: 11.0,
                selectedFontSize: 11.0,
                unselectedItemColor: Colours.text_gray,
                selectedItemColor: Colours.main_icon_color,
                items: [
                  BottomNavigationBarItem(
                    icon:const LoadAssetImage('home/scene', width: 25.0),
                    activeIcon: const LoadAssetImage('home/scene_selected', width: 25.0),
                    title: Text('现场')
                  ),
                  BottomNavigationBarItem(
                    icon:const LoadAssetImage('home/postion', width: 25.0),
                    activeIcon: const LoadAssetImage('home/postion_selected', width: 25.0),
                    title: Text('定位')
                  ),
                  BottomNavigationBarItem(
                    icon:Icon(null),
                    title:Text('拍照'),
                  ),
                  BottomNavigationBarItem(
                    icon:const LoadAssetImage('home/message', width: 25.0),
                    activeIcon: const LoadAssetImage('home/message_selected', width: 25.0),
                    title: Text('消息')
                  ),
                  BottomNavigationBarItem(
                    icon:const LoadAssetImage('home/my', width: 25.0),
                    activeIcon: const LoadAssetImage('home/my_selected', width: 25.0),
                    title: Text('我的')
                  ),
                ],  
                currentIndex: provider.value,
                onTap: _onJumpPage,          
              );
            },
          ),
          body: PageView(
            controller: _pageController,
            onPageChanged: _onPageChanged,
            children: _pageList,
            physics: NeverScrollableScrollPhysics(), // 禁止滑动
          ),
          
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
          floatingActionButton: 
          Container(
            padding: EdgeInsets.all(4.0),
            margin:EdgeInsets.only(top:10.0),
            height:55.0,
            width: 55.0,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(70.0),
              color: Colors.white,
            ),
            child: FloatingActionButton(
              backgroundColor: Colours.main_icon_color,
              elevation: 0.0,
              onPressed: (){
                 _onJumpPage(2);
              },
              child: Icon(Icons.add_a_photo,size:25.0),         
            ),
          ),
          
        ),
      ),
    );
  }

  void _onPageChanged(int index) {
    provider.value = index;
  }

  void _onJumpPage(int index){
    _pageController.jumpToPage(index);
  }

}